project(emulation)

add_subdirectory(mach)

cmake_policy(SET CMP0005 NEW)
enable_language(ASM)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -fvisibility=hidden -fPIC -fno-builtin -ggdb -Wno-int-conversion -Wno-compare-distinct-pointer-types")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib")

add_definitions(-DBSDTHREAD_WRAP_LINUX_PTHREAD
	-DEMULATED_SYSNAME="Darwin"
	-DEMULATED_RELEASE="19.6.0"
	-DEMULATED_VERSION="Darwin Kernel Version 19.6.0"
	-DEMULATED_OSVERSION="19G73"
	-DEMULATED_OSPRODUCTVERSION="10.15.2"
)

# include src/startup for rtsig.h
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_BINARY_DIR}/src/startup
	${CMAKE_BINARY_DIR}/src/kernel/libsyscall
	${CMAKE_CURRENT_BINARY_DIR}
)

mig(signal/mach_exc.defs)
mig(signal/exc.defs)

set(emulation_sources
	${CMAKE_CURRENT_BINARY_DIR}/signal/mach_excUser.c
	elfcalls_wrapper.c
	base.c
	syscalls.c
	simple.c
	errno.c
	readline.c
	common_at.c
	fdpath.c
	wrapped/shm_open.c
	wrapped/shm_unlink.c
	wrapped/sem_open.c
	wrapped/sem_unlink.c
	wrapped/sem_wait.c
	wrapped/sem_trywait.c
	wrapped/sem_close.c
	wrapped/sem_post.c
	guarded/guarded_open_np.c
	guarded/guarded_close_np.c
	guarded/guarded_kqueue_np.c
	machdep/machdeps.c
	machdep/machdep-table.S
	machdep/tls.c
	unistd/write.c
	unistd/read.c
	unistd/pread.c
	unistd/pwrite.c
	unistd/readv.c
	unistd/writev.c
	unistd/flock.c
	unistd/initgroups.c
	mount/unmount.c
	mman/mman.c
	mman/madvise.c
	mman/msync.c
	kqueue/kqueue.c
	kqueue/kevent.c
	kqueue/kevent64.c
	kqueue/kevent_qos.c
	unistd/getsid.c
	unistd/fsync.c
	unistd/sync.c
	unistd/fdatasync.c
	unistd/dup.c
	unistd/dup2.c
	unistd/exit.c
	unistd/close.c
	unistd/fchdir.c
	unistd/fchown.c
	unistd/fchownat.c
	unistd/fchmod.c
	unistd/fchmodat.c
	unistd/getegid.c
	unistd/setgid.c
	unistd/setuid.c
	unistd/settid.c
	unistd/setegid.c
	unistd/seteuid.c
	unistd/setsid.c
	unistd/getuid.c
	unistd/gettid.c
	unistd/geteuid.c
	unistd/getpid.c
	unistd/lseek.c
	unistd/ftruncate.c
	unistd/truncate.c
	unistd/access.c
	unistd/faccessat.c
	unistd/readlink.c
	unistd/readlinkat.c
	unistd/symlink.c
	unistd/symlinkat.c
	unistd/link.c
	unistd/linkat.c
	unistd/unlink.c
	unistd/unlinkat.c
	unistd/chdir.c
	unistd/mknod.c
	unistd/chmod.c
	unistd/chown.c
	unistd/lchown.c
	unistd/umask.c
	unistd/chroot.c
	unistd/getgid.c
	unistd/getppid.c
	unistd/rename.c
	unistd/renameat.c
	unistd/getpgrp.c
	unistd/getdtablesize.c
	unistd/setpgid.c
	unistd/setgroups.c
	unistd/getgroups.c
	unistd/getpgid.c
	unistd/pipe.c
	unistd/chmod_extended.c
	unistd/fchmod_extended.c
	unistd/fchflags.c
	unistd/chflags.c
	unistd/issetugid.c
	select/select.c
	select/pselect.c
	select/poll.c
	process/vfork.c
	process/fork.c
	process/wait4.c
	process/waitid.c
	process/execve.c
	process/posix_spawn.c
	process/getpriority.c
	process/setpriority.c
	signal/duct_signals.c
	signal/kill.c
	signal/sigaltstack.c
	signal/sigaction.c
	signal/sigreturn.c
	signal/sigprocmask.c
	signal/sig_restorer.S
	signal/sigsuspend.c
	signal/sigpending.c
	signal/sigexc.c
	signal/sigwait.c
	misc/ptrace.c
	misc/getentropy.c
	misc/syscall.c
	misc/shared_region_check_np.c
	misc/ioctl.c
	misc/thread_selfid.c
	misc/proc_info.c
	misc/sysctl.c
	misc/sysctl_proc.c
	misc/sysctl_hw.c
	misc/sysctl_kern.c
	misc/sysctl_unspec.c
	misc/sysctl_machdep.c
	misc/sysctl_sysctl.c
	misc/sysctl_net.c
	misc/getrlimit.c
	misc/setrlimit.c
	misc/gethostuuid.c
	misc/getrusage.c
	misc/getlogin.c
	misc/setlogin.c
	misc/reboot.c
	misc/iopolicysys.c
	misc/csops.c
	misc/fileport_makeport.c
	misc/fileport_makefd.c
	misc/csrctl.c
	misc/fsgetpath.c
	misc/abort_with_payload.c
	misc/clonefile.c
	fcntl/open.c
	fcntl/openat.c
	fcntl/fcntl.c
	network/socket.c
	network/socketpair.c
	network/connect.c
	network/recvmsg.c
	network/sendmsg.c
	network/duct.c
	network/recvfrom.c
	network/accept.c
	network/getpeername.c
	network/getsockname.c
	network/shutdown.c
	network/getsockopt.c
	network/setsockopt.c
	network/sendto.c
	network/bind.c
	network/listen.c
	stat/fstat.c
	stat/fstatat.c
	stat/lstat.c
	stat/stat.c
	stat/stat64_extended.c
	stat/lstat64_extended.c
	stat/fstat64_extended.c
	stat/getfsstat.c
	stat/statfs.c
	stat/fstatfs.c
	stat/mkdir.c
	stat/mkdirat.c
	stat/mkfifo.c
	stat/rmdir.c
	stat/common.c
	xattr/getattrlistbulk.c
	xattr/getattrlistat.c
	xattr/getattrlist.c
	xattr/fgetattrlist.c
	xattr/setattrlistat.c
	xattr/setattrlist.c
	xattr/fsetattrlist.c
	xattr/listxattr.c
	xattr/flistxattr.c
	xattr/removexattr.c
	xattr/fremovexattr.c
	xattr/getxattr.c
	xattr/fgetxattr.c
	xattr/setxattr.c
	xattr/fsetxattr.c
	synch/semwait_signal.c
	hfs/stub.c
	dirent/getdirentries.c
	time/gettimeofday.c
	time/utimes.c
	time/futimes.c
	time/setitimer.c
	time/getitimer.c
	ioctl/ioctl.c
	ioctl/termios.c
	ioctl/filio.c
	ioctl/socket.c
	ext/sysinfo.c
	ext/uname.c
	ext/nanosleep.c
	ext/epoll_create.c
	ext/epoll_create1.c
	ext/epoll_ctl.c
	ext/epoll_wait.c
	ext/inotify_init.c
	ext/inotify_init1.c
	ext/inotify_add_watch.c
	ext/inotify_rm_watch.c
	ext/eventfd.c
	ext/eventfd_read.c
	ext/eventfd_write.c
	ext/signalfd.c
	ext/timerfd_create.c
	ext/timerfd_settime.c
	ext/timerfd_gettime.c
	ext/clock_nanosleep.c
	ext/clock_gettime.c
	ext/sched_yield.c
	ext/syslog.c
	ext/futex.c
	ext/mremap.c
	ext/file_handle.c
	ext/fanotify.c
	ext/for-xtrace.c
	ext/for-libelfloader.c
	bsdthread/bsdthread_register.c
	bsdthread/bsdthread_create.c
	bsdthread/bsdthread_ctl.c
	bsdthread/bsdthread_terminate.c
	bsdthread/disable_threadsignal.c
	bsdthread/workq_kernreturn.c
	bsdthread/workq_open.c
	bsdthread/pthread_kill.c
	bsdthread/pthread_chdir.c
	bsdthread/pthread_fchdir.c
	bsdthread/pthread_markcancel.c
	bsdthread/pthread_canceled.c
	psynch/psynch_errno.c
	psynch/psynch_rw_rdlock.c
	psynch/psynch_rw_wrlock.c
	psynch/psynch_rw_unlock.c
	psynch/psynch_cvclrprepost.c
	psynch/psynch_mutexwait.c
	psynch/psynch_mutexdrop.c
	psynch/psynch_cvwait.c
	psynch/psynch_cvbroad.c
	psynch/psynch_cvsignal.c
	psynch/ulock_wait.c
	psynch/ulock_wake.c
	sysv_sem/semget.c
	sysv_sem/semctl.c
	sysv_sem/semop.c
	conf/pathconf.c
	conf/fpathconf.c
	mach/audit_session_self.c
	audit/audit_addr.c
	vchroot_userspace.c
	syscalls-table.S
	linux-syscall.S
)
	
set_source_files_properties(signal/duct_signals.c PROPERTIES COMPILE_FLAGS "-nostdinc")
set_source_files_properties(signal/sigexc.c PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/signal/exc.h)

add_darling_object_library(emulation ${emulation_sources})
add_darling_object_library(emulation_dyld ${emulation_sources})
set_target_properties(emulation_dyld PROPERTIES COMPILE_FLAGS "-ffunction-sections -DVARIANT_DYLD")
add_dependencies(emulation rtsig_h)
add_dependencies(emulation_dyld rtsig_h)

make_fat(emulation)
make_fat(emulation_dyld)

